#include "PlayerStateWallJump.h"
#include "Player.h"


PlayerStateWallJump::PlayerStateWallJump(Player* player, sf::Sound* jumpSFX) : 
PlayerState(player),
_jumpSFX(jumpSFX)
{}

PlayerStateWallJump::~PlayerStateWallJump(void){}

void PlayerStateWallJump::activate()
{
	_player->getAnimationMgr().setAnimation("wall_jump");
	_xLengthStartPos = _player->_position.x;
	_yStartPos = _player->_position.y;
	_xMovementDone = false;
	_wallJumping = true;
	_player->setFlipChangeAllow(false);
	_jumpSFX->play();
}

void PlayerStateWallJump::deactivate()
{
	_player->setFlipChangeAllow(true);
}

int PlayerStateWallJump::update()
{
	const float xLength = 20.0f * _player->getDashMultiplier();

	if (_player->damageReceived())
		return PSRV_TAKE_DAMAGE;
	if (!_xMovementDone)
	{
		if (_player->getFlip() == 1.0f)
		{
			if (_player->_position.x < (_xLengthStartPos + -xLength))
			{
				_player->getAnimationMgr().setAnimation("jump");
				_xMovementDone = true;
				_player->setFlipChangeAllow(true);
			}
			else
				_player->applyVelocity(VA_FLIP_BASED_REVERSE);
		}
		else if (_player->getFlip() == -1.0f)
		{
			if (_player->_position.x > _xLengthStartPos + xLength)
			{
				_player->getAnimationMgr().setAnimation("jump");
				_xMovementDone = true;
				_player->setFlipChangeAllow(true);
			}
			else
				_player->applyVelocity(VA_FLIP_BASED_REVERSE);	
		}
	}
	else
	{
		if (_player->getControlState()->isKeyPressed(CControls::Key(CTRL_DASH, 0), 0))
			return PSRV_DASH_AIR;
		_player->applyVelocity();
	}

	if (_wallJumping)
	{
		_player->_velocity.y = -(_player->getJumpPower() * GameTime::getSingleton()->deltaTime());
		if (_player->_position.y < _yStartPos + _player->getWallJumpLenght())
		{
			_wallJumping = false;
			_player->setFlag(GRAVITY_AFFECTED);
		}
	}

	if (_player->_velocity.y > 0.0f)
		return PSRV_FALLING;



	return PSRV_NOTHING;
}